package org.wat.wcy.isi.mmazur.bp.io.model;

import java.io.IOException;
import java.io.RandomAccessFile;

import org.wat.wcy.isi.mmazur.bp.io.BinaryObject;
import org.wat.wcy.isi.mmazur.bp.io.model.enums.TransitionType;

public class Transition extends BinaryObject {
	
	private IdDesc id;
	private TransitionType type;
	private TransitionCondition condition;

	public Transition() {
		id = new IdDesc();
		type = TransitionType.NULL;
		condition = new TransitionCondition();
	}

	public IdDesc getId() {
		return id;
	}

	public void setId(IdDesc id) {
		this.id = id;
	}

	public TransitionType getType() {
		return type;
	}

	public void setType(TransitionType type) {
		this.type = type;
	}
	
	public TransitionCondition getCondition() {
		return condition;
	}

	public void setCondition(TransitionCondition condition) {
		this.condition = condition;
	}

	@Override
	public void write(RandomAccessFile raf) {
		try {
			raf.writeInt(type.ordinal());
			id.write(raf);
			condition.write(raf);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void read(RandomAccessFile raf) {
		try {
			int index = raf.readInt();
			type = TransitionType.values()[index];
			id.read(raf);
			condition.read(raf);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}
	
	public String toString() {
		return String.format("Transition[id=%s] type=%s", id != null ? id : "NULL",
				type != null ? type.toString() : "NULL");
	}

	public static int sizeOf() {
		return IdDesc.sizeOf() + (int)(Integer.SIZE / 8) + TransitionCondition.sizeOf();
	}
}
